package com.gk.chat.controller;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.gk.chat.common.R;
import com.gk.chat.pojo.Users;
import com.gk.chat.utils.RedisUtil;
import com.gk.chat.utils.ValidateCodeUtils;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/api/v1/common")
public class CommonController {
    @Autowired
    private RedisUtil redisUtil;

//    public CommonController(RedisUtil redisUtil) {
//        this.redisUtil = redisUtil;
//    }

    /**
     * 生成验证码
     * @param user
     * @return
     */
    @PostMapping("/send-sms-code")
    public ResponseEntity<String> sendSmsCode(@RequestBody Users user) {
        String registerMobile = user.getMobile().trim();
        System.out.println("mobile="+registerMobile);

        if(StringUtils.isNotEmpty(registerMobile)){
            //生成随机的4为验证码
            String code= ValidateCodeUtils.generateValidateCode(4).toString();
//            log.info("code={}",code);

            //将验证码存入redis
            redisUtil.setCode(registerMobile,code);

            return ResponseEntity.ok("手机验证码短信发送成功");
        }
        return ResponseEntity.badRequest().body("手机验证码短信发送失败");
    }
}
